<template>
  <div
    v-empty="nopermission"
    class="rc-cont"
    xs-empty-icon="nopermission"
    xs-empty-text="暂无权限">
    <flexbox
      v-if="showEdit"
      class="rc-head"
      direction="row-reverse">
      <el-button
        class="rc-head-item"
        type="primary"
        @click.native="createClick">新建报价
      </el-button>
      <el-button
        class="rc-head-item"
        type="primary"
        @click.native="deleteHandleClick">删除</el-button>
    </flexbox>
    <el-table
      :data="list"
      :height="tableHeight"
      :header-cell-style="headerRowStyle"
      :cell-style="cellStyle"
      stripe
      style="width: 100%;border: 1px solid #E6E6E6;"
      @row-click="handleRowClick"
      @selection-change="selectionList = $event">
      <el-table-column
        v-if="fieldList.length > 0"
        show-overflow-tooltip
        :selectable="checkSelectPreSales"
        key="0"
        type="selection"
        align="center"
        width="55"/>
      <el-table-column
        v-for="(item, index) in fieldList"
        :key="'field_' + index"
        :prop="item.prop"
        :formatter="fieldFormatter"
        :label="item.label"
        show-overflow-tooltip/>
    </el-table>
    <c-r-m-full-screen-detail
      :visible.sync="showFullDetail"
      :id="quotationId"
      crm-type="quotation"/>
    <c-r-m-create-view
      v-if="isCreate"
      :action="createActionInfo"
      crm-type="quotation"
      @save-success="createSaveSuccess"
      @hiden-view="isCreate=false"/>
  </div>
</template>

<script type="text/javascript">
import loading from '../mixins/loading'
import CRMCreateView from './CRMCreateView'
import { crmCustomerQueryContract } from '@/api/customermanagement/customer'
import { crmBusinessQueryQuotation } from '@/api/customermanagement/business'
import {
  delQuotationByIds
} from '@/api/customermanagement/quotation'
import { moneyFormat } from '@/utils'

export default {
  name: 'RelativeQuotation', // 相关联系人  可能再很多地方展示 放到客户管理目录下
  components: {
    CRMFullScreenDetail: () => import('./CRMFullScreenDetail.vue'),
    CRMCreateView
  },
  mixins: [loading],
  props: {
    /** 模块ID */
    id: [String, Number],
    /** 没有值就是全部类型 有值就是当个类型 */
    crmType: {
      type: String,
      default: ''
    },
    /** 是公海 默认是客户 */
    isSeas: {
      type: Boolean,
      default: false
    },
    /** 联系人人下 新建商机 需要联系人里的客户信息  合同下需要客户和商机信息 */
    detail: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      nopermission: false,
      list: [],
      fieldList: [],
      tableHeight: '400px',
      showFullDetail: false,
      isCreate: false, // 控制新建
      quotationId: '', // 查看全屏联系人详情的 ID
      // 创建的相关信息
      createActionInfo: { type: 'relative', crmType: this.crmType, data: {}},
      selectionList: [] // 取消关联勾选的数据
    }
  },
  computed: {
    showEdit(){
      // if(this.crmType == 'business' && this.detail.statusId < 3){
      //   return false
      // }
      if(this.detail.checkStatus != 1){
        return false
      }

      return true;
    }
  },
  watch: {
    id: function(val) {
      this.list = []
      this.getDetail()
    }
  },
  mounted() {
    this.getDetail()
  },
  activated: function() {
    
  },
  deactivated: function() {},
  methods: {
    getFieldList() {
      this.fieldList.push({
        prop: 'quotationNumber',
        width: '200',
        label: '报价单号'
      })
      this.fieldList.push({
        prop: 'quotationItemId',
        width: '200',
        label: '部署方式'
      })
      this.fieldList.push({ prop: 'finalSalesPriceTax', width: '200', label: '总价' })
      this.fieldList.push({
        prop: 'quotationDate',
        width: '200',
        label: '报价日期'
      })

      this.fieldList.push({ prop: 'validPeriod', width: '200', label: '有效期' })
      this.fieldList.push({ prop: 'remark', width: '200', label: '备注' })
      this.fieldList.push({ prop: 'checkStatus', width: '200', label: '状态' })
    },
    getDetail() {
      this.loading = true
      const request = {
        // leads: crmCustomerQueryContract,
        // customer: crmCustomerQueryContract,
        business: crmBusinessQueryQuotation
      }[this.crmType]
      const params = { pageType: 0 }
      params[this.crmType + 'Id'] = this.id
      request(params)
        .then(res => {
          if (this.fieldList.length == 0) {
            this.getFieldList()
          }
          this.nopermission = false
          this.loading = false
          this.list = res.data
        })
        .catch(data => {
          if (data.code == 102) {
            this.nopermission = true
          }
          this.loading = false
        })
    },
    /** 格式化字段 */
    fieldFormatter(row, column) {
      // 如果需要格式化
      if (column.property === 'checkStatus') {
        return this.getStatusName(row.checkStatus)
      } else if (column.property == 'money') {
        return moneyFormat(row[column.property])
      }
      return row[column.property]
    },

    /**
     * 对应的状态名
     */
    getStatusName(status) {
      if (status == 0) {
        return '待审核'
      } else if (status == 1) {
        return '通过'
      } else if (status == 2) {
        return '拒绝'
      } else if (status == 3) {
        return '审核中'
      } else if (status == 4) {
        return '撤回'
      } else if (status == 5) {
        return '未提交'
      } else if (status == 6) {
        return '已作废'
      }
      return ''
    },
    // 当某一行被点击时会触发该事件
    handleRowClick(row, column, event) {
      this.quotationId = row.quotationId
      this.showFullDetail = true
    },
    /** 通过回调控制表头style */
    headerRowStyle({ row, column, rowIndex, columnIndex }) {
      return { textAlign: 'center' }
    },
    /** 通过回调控制style */
    cellStyle({ row, column, rowIndex, columnIndex }) {
      return { textAlign: 'center' }
    },
    /** 新建 */
    createClick() {
      /** 客户 和 商机 下新建合同 */
      if (this.crmType == 'business') {
        this.createActionInfo.data['customer'] = this.detail
        this.createActionInfo.data['business'] = this.detail
      } else if (this.crmType == 'customer') {
        this.createActionInfo.data['customer'] = this.detail
      }
      this.isCreate = true
    },

    // 只有撤回或则未提交的可删除
    checkSelectPreSales(row,index){
      // 审核中、待审核、已审核不能删除
      if(row.checkStatus == 0 || row.checkStatus == 1 || row.checkStatus == 3){
        return false
      }
      return true
    },

    /**
     * 删除设备清单
     */
    deleteHandleClick() {
      if (this.selectionList.length == 0) {
        this.$message.error('请先勾选数据')
      } else {
        this.$confirm('确认删除'+ this.selectionList.length +'条报价单?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            const params = { }
            params.quotationIds = this.selectionList.map(item => { return item.quotationId }).join(',')
            delQuotationByIds(params)
              .then(res => {
                this.getDetail()
                this.$message.success('操作成功')
              })
              .catch(() => {})
          })
          .catch(() => {
            this.$message.info('已取消操作')
          })
      }
    },

    createSaveSuccess() {
      this.getDetail()
    }
  }
}
</script>
<style lang="scss" scoped>
@import '../styles/relativecrm.scss';
</style>
